package com.lyw.idempotentstarter.bean.impl;

import com.lyw.idempotentstarter.bean.IdempotentStorageService;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 本地幂等
 */
public class LocalizeIdempotentStorageService implements IdempotentStorageService {
    private Map<String, Integer> map = null;

    public LocalizeIdempotentStorageService() {
        this.map = new ConcurrentHashMap<>();
    }

    @Override
    public boolean storage(String tenantKey, Long employeeId, String path, String token) {
        map.put(path+tenantKey+employeeId+token, 1);
        return true;
    }

    @Override
    public boolean del(String tenantKey, Long employeeId, String path, String token) {
        return map.remove(path+tenantKey+employeeId+token) != null;
    }

    @Override
    public boolean storage(String tenantKey, String path, String token) {
        map.put(path+tenantKey+token, 1);
        return true;
    }

    @Override
    public boolean del(String tenantKey, String path, String token) {
        return map.remove(path+tenantKey+token) != null;
    }
}
